﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RuoVea.ExDto;
using RuoVea.ExEnum;
using RuoVea.ExSugar.Entity;
using RuoVea.OmiApi.Log.Service;
using RuoVea.OmiApi.Log.Service.Dto;
using RuoVea.OmiLog.Attributes;
using RuoVea.OmiLog.Dto;
using RuoVea.OmiLog.Language;

namespace RuoVea.OmiLog.Controllers;
/// <summary>
/// 异常日志
/// </summary>
[AppRoute]
public class LogExController : Controller
{
    private readonly SysLogExService _thisService;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="thisService"></param>
    public LogExController(SysLogExService thisService )
    {
        _thisService = thisService;
    }
    /// <summary>
    /// 参数配置页面
    /// </summary>
    [HttpGet("index")]
    public IActionResult Index()
    {
        return View();
    }
    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <returns></returns>
    [HttpPost("pages")]
    public async Task<RestfulResult<PageResult<SysLogExDto>>> GetPagesAsync()
    {
        RestfulResult<PageResult<SysLogExDto>> restful = new() { Code = CodeStatus.OK };
        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception(i18n.no_upload_data);
        SysLogExPageParam data = JsonConvert.DeserializeObject<SysLogExPageParam>(dataStr);
        data ??= new SysLogExPageParam { PageNo = 1, PageSize = 10 };
        restful.Data = await _thisService.GetPagesAsync(data);
        return restful;
    }

    /// <summary>
    /// 清空差异日志
    /// </summary>
    /// <returns></returns>
    [HttpPost(nameof(Clear))]
    public RestfulResult Clear()
    {
        RestfulResult restful = new() { Code = CodeStatus.OK, Data = true, };
        _thisService.Clear();
        return restful;
    }

    /// <summary>
    /// 删除指定异常日志🔹
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete(nameof(DeleteDataAsync))]
    public async Task<RestfulResult> DeleteDataAsync([FromBody] EntityBaseId data)
    {
        RestfulResult restful = new() { Code = CodeStatus.OK, Data = true, };
      await  _thisService.DeleteDataAsync(data);
        return restful;
    }
}
